﻿#include "PyLexer.h"
#include "CodeStyleDef.h"

#if USE_QSCI

#include <Qsci/qsciscintilla.h>
#include <Qsci/qsciapis.h>
#include <QDebug>


QStringList py_keywords = {
    "False", "None", "True", "and", "as", "assert", "async", "await",
    "break", "class", "continue", "def", "del", "elif", "else", "except",
    "finally", "for", "from", "global", "if", "import", "in", "is",
    "lambda", "nonlocal", "not", "or", "pass", "raise", "return", "try",
    "while", "with", "yield"
};

QStringList py_functions = {
    // 自定义方法
/*
    "Available", "Exit", "Sleep", "ShowMsg", "SetRetryCnt", "SetRetryTimeout",
    "SendTlv", "SendIIC", "ReadFile", "SelectFile",
    "Toast.info", "Toast.error", "Toast.warning", "Toast.success",
*/
    "board.ReadI2C", "board.WriteI2C",

    // Python 内置方法
    "abs", "all", "any", "ascii", "bin", "bool", "bytearray", "bytes",
    "callable", "chr", "classmethod", "compile", "complex", "delattr",
    "dict", "dir", "divmod", "enumerate", "eval", "exec", "filter",
    "float", "format", "frozenset", "getattr", "globals", "hasattr",
    "hash", "help", "hex", "id", "input", "int", "isinstance", "issubclass",
    "iter", "len", "list", "locals", "map", "max", "memoryview", "min",
    "next", "object", "oct", "open", "ord", "pow", "print", "property",
    "range", "repr", "reversed", "round", "set", "setattr", "slice",
    "sorted", "staticmethod", "str", "sum", "super", "tuple", "type",
    "vars", "zip", "__import__"
};

QStringList py_const = {
/*
    // IIC_STATUS
    "IIC_STATUS.NULL", "IIC_STATUS.BUSY", "IIC_STATUS.END_RECEIVE",
    "IIC_STATUS.END_SEND", "IIC_STATUS.ERR_MASTER_NCK",

    // TlvTag
    "TlvTag.Tlv_seq_id", "TlvTag.Tlv_status", "TlvTag.Tlv_reserve", "TlvTag.Tlv_IIC_feedback", "TlvTag.Tlv_IIC_IIC_CALL",
    "TlvTag.Tlv_Interrupt_resp", "TlvTag.Tlv_MCU_RESET", "TlvTag.Tlv_TOUCH_EVENT", "TlvTag.Tlv_MCU_Product_STATUS",
    "TlvTag.Tlv_MCU_Release_TimeStamp", "TlvTag.Tlv_MCU_RAM_Data", "TlvTag.Tlv_MCU_Product_STD_Frame",
    "TlvTag.Tlv_MCU_Product_EXT_Frame", "TlvTag.Tlv_MCU_Product_REC_Frame", "TlvTag.Tlv_MCU_Warning",
    "TlvTag.Tlv_MCU_String", "TlvTag.Tlv_MCU_Dump", "TlvTag.Tlv_MCU_DumpControl", "TlvTag.Tlv_MCU_ROM_Data",
    "TlvTag.Tlv_MCU_BOOT_APP_REQ", "TlvTag.Tlv_MCU_BOOT_APP_RSP", "TlvTag.Tlv_MCU_Program_REQ", "TlvTag.Tlv_MCU_Program_RSP",
    "TlvTag.Tlv_MCU_Enter_BOOT", "TlvTag.Tlv_MCU_CAN_TX_REQ", "TlvTag.Tlv_MCU_CAN_TX_RSP", "TlvTag.Tlv_MCU_CAN_RX",
    "TlvTag.Tlv_MCU_SET_IO", "TlvTag.Tlv_MCU_ADC_REQ", "TlvTag.Tlv_MCU_ADC_RSP", "TlvTag.Tlv_MCU_REQ_RSP",
    "TlvTag.Tlv_PL_IIC_RSP", "TlvTag.Tlv_IntnCall_RSP", "TlvTag.Tlv_PL_IIC_REQ", "TlvTag.Tlv_IntnCall"
*/
};

PyLexer::PyLexer(QObject *parent)
    : QsciLexerPython(parent)
{
    initKeywordsAndApis();
    initFont();
}

void PyLexer::initFont()
{
    QFont baseFont(CodeStyle::codeBaseFont());
    QFont boldFont(baseFont); boldFont.setBold(true);
    QFont italicFont(baseFont); italicFont.setItalic(true);

    setFont(baseFont);

    // 配置表
    QList<CodeStyle> styles = {
        {QsciLexerPython::Keyword,             boldFont,   Qt::darkYellow},
        {QsciLexerPython::Comment,             baseFont,   Qt::darkGreen},
        {QsciLexerPython::DoubleQuotedString,  baseFont,   Qt::darkRed},
        {QsciLexerPython::SingleQuotedString,  baseFont,   Qt::darkRed},
        {QsciLexerPython::Number,              baseFont,   Qt::darkBlue},
        {QsciLexerPython::Identifier,          baseFont,   Qt::darkCyan},
        {QsciLexerPython::Operator,            baseFont,   Qt::black},
    };

    // 应用配置
    for (const auto &s : styles) {
        setFont(s.font, s.type);
        setColor(s.color, s.type);
    }
}

void PyLexer::initKeywordsAndApis()
{
    QsciAPIs* apis = new QsciAPIs(this);
    // 添加一些 API（可以自己扩展）
    for (QString& s : py_functions) {
        apis->add(s);
    }

    for (QString& s : py_const) {
        apis->add(s);
    }

    for (QString& s : py_keywords) {
        apis->add(s);
    }

    // 编译 API 列表
    apis->prepare();
}

const char *PyLexer::keywords(int set) const
{
    if (set == 1) {
        return py_keywords.join(" ").toLatin1();
    }
    return "";
}

#endif // USE_QSCI

